package cn.edu.sdjzu.xg.xkgl.controller.eduadmin;

import cn.edu.sdjzu.xg.xkgl.domain.CourseSelection;
import cn.edu.sdjzu.xg.xkgl.service.CourseSelectionService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

@WebServlet("/courseDetailController")
public class CourseDetailController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
            int id = Integer.parseInt(request.getParameter("id"));
            Collection<CourseSelection> courseSelections = CourseSelectionService.getCourseSelectionService().findByCourseId(id);
            //获取students属性值
            request.setAttribute("courseSelections",courseSelections);
            request.setAttribute("courseId",id);
            //请求转发到
            request.getRequestDispatcher("pages/eduadmin/selection/courseDetail.jsp").forward(request,response);
        }catch (SQLException e){
            e.printStackTrace();
        }

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
            StringBuilder conditionSb =  new StringBuilder();
            //获取请求参数string值
            String string = request.getParameter("string");
            conditionSb.append(" course_id = ")
                    .append(request.getParameter("courseId") )
                    .append(" and student_id in (select id from student where name like '%")
                    .append(string)
                    .append("%' ")
                    .append("or no like '%")
                    .append(string)
                    .append("%')");
            //获取所有的Student对象
            Collection<CourseSelection> courseSelections = CourseSelectionService.getCourseSelectionService().findAll(conditionSb.toString());
            int id = Integer.parseInt(request.getParameter("courseId"));
            request.setAttribute("courseId",id);
            request.setAttribute("courseSelections",courseSelections);
            //请求转发到list.jsp
            request.getRequestDispatcher("pages/eduadmin/selection/courseDetail.jsp").forward(request,response);
        }catch (SQLException e) {
            //异常处理，把错误信息保存在request的message属性中，请求转发到error.jsp
            e.printStackTrace();
            request.setAttribute("message","查询失败");
            request.getRequestDispatcher("/pages/error.jsp").forward(request,response);
        }
    }
}
